﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Tank
{


    class Tank
    {
        public int x;
        public int y;
        public int smer = 0; //0 - hore   1 - dole   2 - dolava   3 - doprava
        public int pocet = 0;
        public static int sizeX, sizeY;
        public int pozX, pozY;
        public Strela s;
        char c;
        private int mapHeight;
        private int mapWidth;
        public int team; //0-player 1-bot

        public Tank()
        {
            s = null;
            sizeX = Map.sirka * 8 / 10;
            sizeY = Map.vyska * 8 / 10;
            this.mapWidth = Map.x * Map.sirka;
            this.mapHeight = Map.y * Map.vyska;
            this.pozX = 3;
            this.pozY = 9;
            this.x = pozX * Map.sirka;
            this.y = pozY * Map.vyska;
            this.smer = 0;
            this.share();
            //this.Move(smer);

        }

        public Tank(int x, int y)
        {
            s = null;
            sizeX = Map.sirka * 8 / 10;
            sizeY = Map.vyska * 8 / 10;
            this.mapWidth = Map.x * Map.sirka;
            this.mapHeight = Map.y * Map.vyska;
            this.pozX = x;
            this.pozY = y;
            this.x = pozX * Map.sirka;
            this.y = pozY * Map.vyska;
            this.smer = 0;
            //this.Move(smer);
            this.share();

        }

        public void Move(int smer)
        {

            this.smer = smer;

            switch (smer)
            {

                case 0: if ((this.y >= 5) && isFreeTank(0)) this.y -= 5;
                    break;
                case 1: if (this.y <= mapHeight - sizeY - 5 && isFreeTank(1)) this.y += 5;
                    break;
                case 2: if (this.x >= 5 && isFreeTank(2)) this.x -= 5;
                    break;
                case 3: if (this.x <= mapWidth - sizeX - 5 && isFreeTank(3)) this.x += 5;
                    break;
            }

            this.pozX = getX(this.x + (Tank.sizeX / 2));
            this.pozY = getY(this.y + (Tank.sizeY / 2));

        }

        public void share()
        {
            for (int k = 0; k < 10; k++)
            {
                for (int l = 0; l < 10; l++)
                {
                    //                    str += Share.opp[i, j];
                    Share.opp[k, l] = false;
                }
                //                str += "\n";
            }
            Share.tankX = this.pozX;
            Share.tankY = this.pozY;
            Share.opp[this.pozY, this.pozX] = true;

            int a;
            int b;

            a = this.pozY - 1;
            b = this.pozX;
            while (a >= 0 && Map.map[a, b] != 'P')
            {
                Share.opp[a, b] = true;
                a--;
            }

            a = this.pozY + 1;
            b = this.pozX;
            while (a < Map.x && Map.map[a, b] != 'P')
            {
                Share.opp[a, b] = true;
                a++;
            }

            a = this.pozY;
            b = this.pozX - 1;
            while (b >= 0 && Map.map[a, b] != 'P')
            {
                Share.opp[a, b] = true;
                b--;
            }

            a = this.pozY;
            b = this.pozX + 1;

            while (b < Map.y && Map.map[a, b] != 'P')
            {
                Share.opp[a, b] = true;
                b++;
            }
        }

        public void shot()
        {
            Strela s2 = s;
            if (s == null)
            {
                s = new Strela(0, 0, this.smer);
                s2 = s;
            }
            else
            {

                while (s2.next != null)
                {
                    s2 = s2.next;
                }
                s2.next = new Strela(0, 0, this.smer);
                s2 = s2.next;
            }

            switch (this.smer)
            {
                case 0:
                    {
                        s2.x = this.x + (sizeX / 2);
                        s2.y = this.y;
                        break;
                    }
                case 1:
                    {
                        s2.x = this.x + (sizeX / 2);
                        s2.y = this.y + sizeY;
                        break;
                    }
                case 2:
                    {
                        s2.x = this.x;
                        s2.y = this.y + (sizeY / 2);
                        break;
                    }
                case 3:
                    {
                        s2.x = this.x + sizeX;
                        s2.y = this.y + (sizeY / 2);
                        break;
                    }
            }
        }

        public void ShotMove()
        {
            Strela head = new Strela(0, 0, 0);
            Strela pom = s;
            Strela pom2 = null;
            head.next = pom;

            while (pom != null)
            {
                //int pomx = pom.x;
                //int pomy = pom.y;

                switch (pom.smer)
                {

                    case 0:
                        {
                            if (pom.y >= 15) pom.y -= 15;
                            else
                            {
                                if (pom2 != null)
                                    pom2.next = pom.next;
                                else
                                    s = pom.next;
                            }

                            break;
                        }
                    case 1:
                        {
                            if (pom.y <= Map.vyska * Map.y - 16) pom.y += 15;
                            else
                            {
                                if (pom2 != null)
                                    pom2.next = pom.next;
                                else
                                    s = pom.next;
                            } break;
                        }
                    case 2:
                        {
                            if (pom.x >= 15) pom.x -= 15;

                            else
                            {
                                if (pom2 != null)
                                    pom2.next = pom.next;
                                else
                                    s = pom.next;
                            } break;
                        }
                    case 3:
                        {
                            if (pom.x <= Map.sirka * Map.x - 16) pom.x += 15;
                            else
                            {
                                if (pom2 != null)
                                    pom2.next = pom.next;
                                else
                                    s = pom.next;
                            }
                            break;
                        }


                }

                if (this.team == 0)
                {
                    if (isTank(pom.x, pom.y) == 1)
                    {
                        Share.reset_bot1 = true;
                        if (pom2 != null)
                            pom2.next = pom.next;
                        else
                            this.s = pom.next;
                    }

                    if (isTank(pom.x, pom.y) == 2)
                    {
                        Share.reset_bot2 = true;
                        if (pom2 != null)
                            pom2.next = pom.next;
                        else
                            this.s = pom.next;
                    }
                }
                else
                {
                    if (isTank(pom.x, pom.y) == 0)
                    {
                        Share.reset_player = true;

                        if (pom2 != null)
                            pom2.next = pom.next;
                        else
                            this.s = pom.next;
                    }

                }

                c = getMap(pom.y, pom.x);

                //str = c.ToString();

                if (c == 'P')
                {
                    if (pom2 != null)
                        pom2.next = pom.next;
                    else
                        s = pom.next;
                }

                pom2 = pom;
                pom = pom.next;
                head = head.next;
            }

        }

        private bool isFreeTank(int smer)
        {
            int x = this.x;
            int y = this.y;

            int smooth = 5;
            int t = 5;
            switch (smer)
            {
                case 0:
                    {
                        if (getMap(this.y - t, this.x + smooth) == 'P' || getMap(this.y - t, this.x + Tank.sizeX - smooth) == 'P') return false;
                        break;
                    }
                case 1:
                    {
                        if (getMap(this.y + Tank.sizeY + t, this.x + smooth) == 'P' || getMap(this.y + Tank.sizeY + t, this.x + Tank.sizeX - smooth) == 'P') return false;
                        break;
                    }
                case 2:
                    {
                        if (getMap(this.y + smooth, this.x - t) == 'P' || getMap(this.y + Tank.sizeY - smooth, this.x - t) == 'P') return false;
                        break;
                    }
                case 3:
                    {
                        if (getMap(this.y + smooth, this.x + Tank.sizeX + t) == 'P' || getMap(this.y + Tank.sizeY - smooth, this.x + Tank.sizeX + t) == 'P') return false;
                        break;
                    }
            }
            return true;
        }

        private char getMap(int x, int y)
        {
            int i = x / Map.vyska;
            int j = y / Map.sirka;
            char c = Map.map[i, j];
            return c;
        }

        public static int getX(int X)
        {
            X = X / Map.sirka;
            return X;
        }

        public static int getY(int Y)
        {
            Y = Y / Map.vyska;
            return Y;
        }

        public int isTank(int x, int y)
        {
            //if (this.team == 0)
            //{
            if (Share.bot1.x < x && Share.bot1.x + Tank.sizeX > x && Share.bot1.y < y && Share.bot1.y + Tank.sizeY > y)
            {
                return 1;
            }
            if (Share.bot2.x < x && Share.bot2.x + Tank.sizeX > x && Share.bot2.y < y && Share.bot2.y + Tank.sizeY > y)
            {
                return 2;
            }
            //}
            //else
            //{
            if (Share.player.x < x && Share.player.x + Tank.sizeX > x && Share.player.y < y && Share.player.y + Tank.sizeY > y)
            {
                return 0;
            }
            //}
            return 3;
        }
    }

}